﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using Learn.Remoting.Server;
using System.Runtime.Remoting.Channels.Http;

namespace Learn.Remoting.Client
{
    public class MyRemotingClient
    {
        public static void ActivatedClient()
        {
            #region
            // Register TCP Channel.
            ChannelServices.RegisterChannel(new TcpChannel());

            // Create activated client type entry.
            ActivatedClientTypeEntry myActivatedClientTypeEntry =
                new ActivatedClientTypeEntry(typeof(MyRemotingHelloServer),
                "tcp://localhost:4321");

            // Register type on client to activate it on the server.
            RemotingConfiguration.RegisterActivatedClientType(
                myActivatedClientTypeEntry);

            // Activate a client activated object type.
            MyRemotingHelloServer myHelloServer = new MyRemotingHelloServer("");

            // Print the object type.
            Console.WriteLine(
                "Object type of client activated object: " +
                myActivatedClientTypeEntry.ObjectType.ToString());

            // Print the application URL.
            Console.WriteLine(
                "Application url where the type is activated: " +
                myActivatedClientTypeEntry.ApplicationUrl);

            // Print the string representation of the type entry.
            Console.WriteLine(
                "Type name, assembly name and application URL " +
                "of the remote object: " +
                myActivatedClientTypeEntry.ToString());

            // Print a blank line.
            Console.WriteLine();

            // Check that server was located.
            if (myHelloServer == null)
            {
                Console.WriteLine("Could not locate server");
            }
            else
            {
                Console.WriteLine("Calling remote object");
                Console.WriteLine(myHelloServer.HelloMethod("Bill"));
                Console.WriteLine(myHelloServer.Count());
                Console.WriteLine(myHelloServer.Count());
            }
            #endregion
        }

        public static void WellKnownService()
        {
            #region
            // Create a 'HttpChannel' object and  register with channel services.
            ChannelServices.RegisterChannel(new HttpChannel());
            Console.WriteLine(" Start calling from Client One.......");

            WellKnownClientTypeEntry myWellKnownClientTypeEntry =
                           new WellKnownClientTypeEntry(typeof(MyRemotingHelloServer),
                                           "http://localhost:1234/SayHello");
            myWellKnownClientTypeEntry.ApplicationUrl = "http://localhost:1234/SayHello";
            RemotingConfiguration.RegisterWellKnownClientType(myWellKnownClientTypeEntry);

            WellKnownClientTypeEntry myWellKnownClientTypeEntry2 =
                          new WellKnownClientTypeEntry(typeof(MyRemoteObject),
                                          "http://localhost:1234/MyRemoteObject");
            myWellKnownClientTypeEntry.ApplicationUrl = "http://localhost:1234/MyRemoteObject";
            RemotingConfiguration.RegisterWellKnownClientType(myWellKnownClientTypeEntry2);

            // Get the proxy object for the remote object.
            MyRemotingHelloServer myHelloServerObject = new MyRemotingHelloServer("");
            // Retrieve an array of object types registered on the 
            // client end as well-known types.
            WellKnownClientTypeEntry[] myWellKnownClientTypeEntryCollection =
                   RemotingConfiguration.GetRegisteredWellKnownClientTypes();
            Console.WriteLine("The Application Url to activate the Remote Object :"
                 + myWellKnownClientTypeEntryCollection[0].ApplicationUrl);
            Console.WriteLine("The 'WellKnownClientTypeEntry' object :"
                     + myWellKnownClientTypeEntryCollection[0].ToString());
            // Make remote method calls.
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine(myHelloServerObject.HelloMethod(" Client One"));
                Console.WriteLine(myHelloServerObject.Count());
            }


            MyRemoteObject mro = new MyRemoteObject();
            Console.WriteLine(mro.Count());
            Console.WriteLine(mro.Count());

            #endregion
        }  
    }
}
